package com.example.lss.news;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lss.news.adapter.TabViewPager;
import com.example.lss.news.bean.Data;
import com.example.lss.news.bean.Ress;
import com.example.lss.news.bean.Result;
import com.example.lss.news.bean.WeatherResult;
import com.example.lss.news.dao.NewsDao;
import com.example.lss.news.fragment.CaiJingFragmet;
import com.example.lss.news.fragment.GuoJiFragmet;
import com.example.lss.news.fragment.GuoNeiFragmet;
import com.example.lss.news.fragment.JunShiFragmet;
import com.example.lss.news.fragment.KeJiFragmet;
import com.example.lss.news.fragment.SheHuiFragmet;
import com.example.lss.news.fragment.ShiShangFragmet;
import com.example.lss.news.fragment.TiYuFragmet;
import com.example.lss.news.fragment.TopFragmet;
import com.example.lss.news.fragment.YuLeFragmet;
import com.example.lss.news.utils.JsonUtils;
import com.example.lss.news.utils.app.BaseActivity;
import com.example.lss.news.utils.app.BaseFrament;
import com.example.lss.news.utils.http.HttpUtils;
import com.example.lss.news.utils.http.JsonCallback;
import com.example.lss.news.utils.pic.CircleImageView;
import com.example.lss.news.utils.pic.GlideApp;
import com.github.mzule.fantasyslide.FantasyDrawerLayout;

import java.util.List;

/**
 * 主界面
 */
public class MainActivity extends BaseActivity {

    private DrawerLayout dl;
    private NavigationView nv;
    private Toolbar toolbar;
    private TabLayout tab;
    private ViewPager vp;
    private TextView weather_city;
    private TextView weather;
    private TextView weather_temperature;
    private ImageView img;
    private TextView weather_date;
    private TextView weather_week;
    private WeatherResult weatherResult;
    //    top(头条，默认),shehui(社会),guonei(国内),guoji(国际),yule(娱乐),tiyu(体育)junshi(军事),keji(科技),caijing(财经),shishang(时尚)
    private String[] type = new String[]{"top", "shehui", "guonei", "guoji", "yule", "tiyu", "junshi", "keji", "caijing", "shishang"};
    private String[] typename = new String[]{"头条", "社会", "国内", "国际", "娱乐", "体育", "军事", "科技", "财经", "时尚"};
    private BaseFrament[] baseFrament = new BaseFrament[]{new TopFragmet(),
            new SheHuiFragmet(), new GuoNeiFragmet(), new GuoJiFragmet(),
            new YuLeFragmet(), new TiYuFragmet(), new JunShiFragmet(),
            new KeJiFragmet(), new CaiJingFragmet(), new ShiShangFragmet()
    };
    private TabViewPager tabViewPager;


    @Override
    protected int getLayoutId() {

        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        tab = findViewById(R.id.tab);
        vp = findViewById(R.id.vp);
        dl = findViewById(R.id.dl);
        nv = findViewById(R.id.nv);
//        showToast(getSP("head", null));

        weather_city = findViewById(R.id.weather_city);
        weather = findViewById(R.id.weather);
        weather_temperature = findViewById(R.id.weather_temperature);
        img = findViewById(R.id.img);
        weather_date = findViewById(R.id.weather_date);
        weather_week = findViewById(R.id.weather_week);
        toolbar = findViewById(R.id.Toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.mipmap.ic_menu);
        }
        final String acc = getSP("head", null);
        final NewsDao dao = new NewsDao();
        View view = nv.inflateHeaderView(R.layout.nav_header);
        CircleImageView head = view.findViewById(R.id.icon_image);
        TextView name = view.findViewById(R.id.username);
        TextView qianming = view.findViewById(R.id.mail);

        name.setText(dao.showone(acc).get(0).getName());
        qianming.setText(dao.showone(acc).get(0).getText());

        String url = dao.showone(acc).get(0).getImg().substring(25);
        GlideApp.with(this).load(url).into(head);
        head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
        //默认选中
        nv.setCheckedItem(R.id.nav_call);

        nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                int id = item.getItemId();
                if (id == R.id.nav_friends) {
                    Intent it = new Intent(MainActivity.this, VideoActivity.class);
                    startActivity(it);
                } else if (id == R.id.nav_task) {
                    Intent it = new Intent(MainActivity.this, AboutActivity.class);
                    startActivity(it);
                }
                dl.closeDrawers();
                return true;
            }
        });


        //tab可滚动
        tab.setTabMode(TabLayout.MODE_SCROLLABLE);
        //tab居中显示
        tab.setTabGravity(TabLayout.GRAVITY_CENTER);
        //tab的字体选择器,默认黑色,选择时红色
        tab.setTabTextColors(Color.BLACK, Color.RED);
        //tab的下划线颜色,默认是藍色
        tab.setSelectedTabIndicatorColor(Color.BLUE);
        tabViewPager = new TabViewPager(getSupportFragmentManager(), typename, baseFrament);
        vp.setAdapter(tabViewPager);
        vp.setOffscreenPageLimit(baseFrament.length - 1);
        tab.setupWithViewPager(vp);
        init();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }


    public void init() {

        new Thread() {
            @Override
            public void run() {
                super.run();
                HttpUtils.get("http://v.juhe.cn/weather/index?cityname=郑州&key=6d195239691f5fe815e3d6b9f0726dc0", new JsonCallback() {
                    @Override
                    public void onStart(Object tag) {

                    }

                    @Override
                    public void onSuccess(String json) {
                        weatherResult = JsonUtils.toBean(json, JsonUtils.newParamType(WeatherResult.class));
                        weather_city.setText(weatherResult.getResult().getToday().getCity());
                        weather_temperature.setText(weatherResult.getResult().getToday().getTemperature());
                        String wea = weatherResult.getResult().getToday().getWeather();
                        weather.setText(weatherResult.getResult().getToday().getWeather());
                        weather_week.setText(weatherResult.getResult().getToday().getWeek());
                        weather_date.setText(weatherResult.getResult().getToday().getDate_y());
                        if (wea.equals("多云")) {
                            img.setImageResource(R.mipmap.yin);
                        } else if (wea.equals("多云转晴")) {
                            img.setImageResource(R.mipmap.duoyun);
                        } else if (wea.equals("晴") || wea.equals("炎热")) {
                            img.setImageResource(R.mipmap.clear);
                        } else {
                            img.setImageResource(R.mipmap.rain);
                        }
                    }

                    @Override
                    public void onError(Throwable exception) {

                    }

                    @Override
                    public void onFinish() {

                    }
                });
            }
        }.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                dl.openDrawer(GravityCompat.START);

                break;
            case R.id.search:
                Intent it = new Intent(this, SearchActivity.class);
                startActivity(it);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
